from typing import TypedDict,List,Dict,Any
from langchain_core.messages import BaseMessage, HumanMessage, AIMessage, trim_messages


class AgentState(TypedDict):
    messages: List[BaseMessage] #消息列表
    intermediate_results :Dict[str,Any] #短期记忆的其他数据

def add_user_message(state:AgentState,user_message:str) -> Dict[str,List[BaseMessage]]:
    """向对话历史记录添加用户信息"""
    new_message = HumanMessage(content=user_message)
    return {"messages":state["messsages"] +[new_message]}

def add_ai_message(state:AgentState,ai_response:str) -> Dict[str,List[BaseMessage]]:
    """向对话记录添加ai响应"""
    new_message = AIMessage(content=ai_response)
    return {"messages":state["messages"] +[new_message]}

def truncate_history(state:AgentState,max_message:int) -> Dict[str,List[BaseMessage]]:
    truncated = state["messages"][-max_message]
    return {"messages":truncated}

def truncate_by_token(state:AgentState,max_tokens:int) -> Dict[str,List[BaseMessage]]:
    """使用langchain的trim_message根据次元修剪历史记录"""
    trimed = trim_messages()
    return ""

#摘要  利用大模型生成摘要